package beans;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import model.History;

import dao.HistoryDao;
import dao.ServerLogEntryDao;
import dao.UserDao;
import dto.HistoryDTO;
import enumerations.LogType;
import enumerations.Type;

/**
 * Session Bean implementation class HistoryBean
 * @author zahariem
 * 
 * add,delete,view operations for history
 * 
 */
@Stateless
public class HistoryBean implements HistoryBeanRemote {
	
	@EJB
	HistoryDao hDao;
	
	@EJB
	ServerLogEntryDao daoSLE;
	
	@EJB
	UserDao daoUser;

    /**
     * Default constructor. 
     */
    public HistoryBean() {
    }

	
	/**
	 * Insereaza in tabela history o inregistrare conform u datele primite ca parametru 
	 * 
	 * @author zahariem
	 * @param idUser userul care face operatia
	 * @param idSource resursa pe care se face operatia
	 * @param type un String cu valoarea egala cu unul din elementele din enum.ul {@link enumerations.Type}
	 * @param description este o descriere despre modificarea sau crearea resursei
	 * @param date data la care s-a efectuat op 
	 */
	@Override
	public void addChangeToHistory(int idUser, int idSource, Type type, String description, Date date) {
		/*Type typeReceived = null;
		for (Type logtype : Type.values()) {
			if (logtype.toString().equals(type)) {
				typeReceived = logtype;
			}
		}*/
		hDao.addChangeToHistory(idUser, idSource, type, description, date);	
		
		LogType logTypeReceived = null;
		for (LogType logtype : LogType.values()) {
			if (logtype.toString().equals(type)) {
				logTypeReceived = logtype;
			}
		}
		daoSLE.insertOpToLog(logTypeReceived, description, date, daoUser.getUserById(idSource));
	}

	/**
	 * Sterge toate inregistrarile din tabela history care au id_folder(de fapt id de resursa)
	 * egal cu parametrul primit si pune actiunea in serverLogEntry
	 * 
	 * @author zahariem
	 * @param idSource id-ul resursei pentru care vreau sa sterg istoria
	 * @param author  supervisor sau administrator 
	 */
	@Override
	public void removeHistory(int idSource,int author) {		
		hDao.removeHistory(idSource);
		daoSLE.insertOpToLog(LogType.delete_resource, "Resource deleted", new Date(), daoUser.getUserById(author));
	}

	/**
	 * Returneaza toate inregistrarile pentru resursa cu id-ul primit ca parametru
	 * 
	 * @author zahariem
	 * @param idSource este id-ul resursei 
	 * @return o lista de obiecte de tip {@link dto.HistoryDTO} 
	 */
	@Override
	public List<HistoryDTO> allHistoryToSource(int idSource) {
		List<HistoryDTO> lToReturn= new ArrayList<HistoryDTO>();
		
		List<History> lH=hDao.allHistoryToSource(idSource);
		for (History h : lH) {
			HistoryDTO hDto = new HistoryDTO();
			hDto.setId(h.getId());
			hDto.setDescription(h.getDescription());
			hDto.setType(h.getType().toString());
			hDto.setData(h.getDate());
			hDto.setAuthorStr(h.getUser().getName());
			lToReturn.add(hDto);
		}		
		return lToReturn;
	}
}
